<template>
  <div>
    <el-form :model="form" :rules="rules" ref="form" class="form">
      <el-form-item prop="username">
        <el-input v-model="form.username" placeholder="用户名手机"></el-input>
      </el-form-item>
      <el-form-item class="form-item" prop="captcha">
        <el-input placeholder="验证码" v-model="form.captcha">
          <template slot="append">
            <el-button @click="handleSendCaptcha">
              发送验证码
            </el-button>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="nickname">
        <el-input v-model="form.nickname" placeholder="昵称"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="form.password" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item prop="checkPassword">
        <el-input
          v-model="form.checkPassword"
          placeholder="确认密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleRegSubmit">立即注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    const passwordValidator = (rule, value, callback) => {
      // 在一个校验函数内部, 可以接受三个参数
      // 1. 规则对象本身
      // 2. 当前输入的值
      // 3. 放行的回调函数
      // 当你校验完输入值的合法性, 需要调用 callback 继续下去
      // 我们在这里需要做的就是判断当前二次代码的输入值是否跟第一次一样
      // 不通过的情况下,需要在调用 callback 的时候, 传入一个错误对象
      // new Error('请再次输入密码')
      if (value == "") {
        // 二次代码根本没有输入, 应该报错请再次输入密码
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.password) {
        // 二次代码跟第一次不一样, 应该报错两次输入的密码应该相同
        callback(new Error("两次密码输入不一致"));
      } else {
        // 通过
        callback();
      }
    };
    return {
      form: {
        username: "", // 手机号码
        nickname: "", // 昵称
        captcha: "", //手机验证码
        password: "", // 密码
        checkPassword: "" // 确认密码
      },
      rules: {
        username: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
          { min: 11, max: 11, message: "长度为11 个字符", trigger: "blur" }
        ],
        nickname: [{ required: true, message: "请输入昵称", trigger: "blur" }],
        captcha: [{ required: true, message: "请输入验证码", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        checkPassword: [{ validator: passwordValidator, trigger: "blur" }]
      }
    };
  },
  methods: {
    // 发送验证码
    handleSendCaptcha() {
      if (!this.form.username) {
        this.$confirm("手机号码不能为空", "提示", {
          confirmButtonText: "确定",
          showCancelButton: false,
          type: "warning"
        });
        return;
      }
      if (this.form.username.length !== 11) {
        this.$confirm("手机号码格式错误", "提示", {
          confirmButtonText: "确定",
          showCancelButton: false,
          type: "warning"
        });
        return;
      }
      // 请求发送验证码的接口
      this.$store
        .dispatch("user/sendCaptchag", this.form.username)
        .then(code => {
          this.$confirm(`手机验证码为${code}`, {
            confirmButtonText: "确定",
            showCancelButton: false,
            type: "warning"
          });
        });
    },
    // 注册
    handleRegSubmit() {
      const { checkPassword, ...form } = this.form;
      console.log(form);
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$axios({
            url: "/accounts/register",
            method: "post",
            data: form
          }).then(res => {
            console.log(res);
            if (res.data.token) {
              this.$emit("login");
              this.$message.success("注册成功 正在跳转......");
              // 注册成功后跳转到登录页
              setTimeout(() => {
                this.$router.push("/user/login");
              }, 1000);
            }
          });
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.form {
  padding: 25px;

  .el-button--primary {
    width: 100%;
  }
}
</style>
